package com.zhouyu.exampledesignmoudle.singleton;

/**
 * @Author:zhouyu
 * @create: 9:43 2019/12/21
 * @Description: 懒汉单例-双重锁模式
 */
public class LayzSingleton1 {

    //使用原子变量，保证内存可见性
    private  static volatile LayzSingleton1 singleton;

    private LayzSingleton1(){

    }

    public static LayzSingleton1 getInstance(){
        //避免不必要的检查
        if(singleton==null){
            synchronized(LayzSingleton1.class){
                //拿到线程后的例行检查
                if(singleton==null){
                    singleton=new LayzSingleton1();
                }
            }
        }
        return singleton;
    }
}
